﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace MVAScratchPad.Common
{
    public class TextBoxExtension : DependencyObject
    {
        public static SearchBox GetSearchBox(DependencyObject obj) { return (SearchBox)obj.GetValue(SearchBoxProperty); }
        public static void SetSearchBox(DependencyObject obj, SearchBox value) { obj.SetValue(SearchBoxProperty, value); }
        public static readonly DependencyProperty SearchBoxProperty =
            DependencyProperty.RegisterAttached("SearchBox", typeof(SearchBox), typeof(TextBoxExtension), new PropertyMetadata(null, SearchBoxChanged));
        private static void SearchBoxChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var textBox = d as TextBox;
            if (textBox == null)
                throw new InvalidCastException("This extension is for TextBox only");
            var searchBox = e.NewValue as SearchBox;
            if (searchBox == null)
                return;
            searchBox.QuerySubmitted += (s, args) =>
            {
                var text = args.QueryText;
                if (string.IsNullOrEmpty(text))
                {
                    textBox.SelectionLength = 0;
                    return;
                }
                var index = textBox.Text.IndexOf(text);
                if (index < 0)
                {
                    textBox.SelectionLength = 0;
                    return;
                }
                textBox.SelectionStart = index;
                textBox.SelectionLength = text.Length;
                if (textBox.FocusState == FocusState.Unfocused)
                    textBox.Focus(FocusState.Programmatic);
            };
        }

        public static string GetSelectThis(DependencyObject obj) { return (string)obj.GetValue(SelectThisProperty); }
        public static void SetSelectThis(DependencyObject obj, string value) { obj.SetValue(SelectThisProperty, value); }
        public static readonly DependencyProperty SelectThisProperty =
            DependencyProperty.RegisterAttached("SelectThis", typeof(string), typeof(TextBoxExtension), new PropertyMetadata(string.Empty, SelectThisChange));
        private static void SelectThisChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var textBox = d as TextBox;
            if (textBox == null)
                throw new InvalidCastException("This extension is for TextBox only");
            if (e.NewValue == null)
                return;
            var text = e.NewValue.ToString();
            if (string.IsNullOrEmpty(text))
            {
                textBox.SelectionLength = 0;
                return;
            }
            var index = textBox.Text.IndexOf(text);
            if (index < 0)
            {
                textBox.SelectionLength = 0;
                return;
            }
            textBox.SelectionStart = index;
            textBox.SelectionLength = text.Length;
            if (textBox.FocusState == FocusState.Unfocused)
                textBox.Focus(FocusState.Programmatic);
        }
    }
}
